#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

// #define READ_FILE

short g_city[100][100];
short g_dp[100][100];

int calc(int line, int row, int cityLen, int step)
{
	if (g_dp[line][row] != -1)
	{
		return g_dp[line][row];
	}
	
	int max = 0;
	for (int i = line-step; i <= line+step; ++i)
	{
		if (i < 0 || i == line || i >= cityLen || g_city[i][row] <= g_city[line][row]) { continue; }
		short res = calc(i, row, cityLen, step);
		if (res > max)
		{
			max = res;
		}
	}
	for (int i = row-step; i <= row+step; ++i)
	{
		if (i < 0 || i == row || i >= cityLen || g_city[line][i] <= g_city[line][row]) { continue; }
		short res = calc(line, i, cityLen, step);
		if (res > max)
		{
			max = res;
		}
	}
	
	g_dp[line][row] = g_city[line][row] + max;
	
	return g_dp[line][row];
}

int main(int argc, char **argv)
{
#ifdef READ_FILE
	ifstream readIn("1107.input");
	if (!readIn)
	{
		readIn.open("../1107.input");
	}
#else
	#define readIn cin
#endif

	short n, k;
	readIn >> n >> k;
	while (n != -1 && k != -1)
	{
		memset(g_dp, 0xff, sizeof(g_dp));
		
		for (int line = 0; line < n; ++line)
		{
			for (int row = 0; row < n; ++row)
			{
				readIn >> g_city[line][row];
			}
		}
		
		int res = calc(0, 0, n, k);
		
		cout << res << endl;
		
		readIn >> n >> k;
	}
}